package org.nvframe.camera.plugin;

import org.newdawn.slick.GameContainer;
import org.nvframe.camera.Camera;
import org.nvframe.component.state.Position;
import org.nvframe.entity.Entity;
import org.nvframe.event.EventService;
import org.nvframe.event.eventtype.UpdateEvent;
import org.nvframe.event.eventtype.UpdateListener;

public class FocusOnEntityPlugin implements CameraPlugin, UpdateListener {
	
	private Camera camera;
	private Entity focusEntity;
	
	public FocusOnEntityPlugin(Entity focusEntity) {
		this.focusEntity = focusEntity;
		
		EventService.getInstance().addEventListener(this);
	}
	
	public void setCamera(Camera camera) {
		this.camera = camera;
	}
	
	@Override
	public void onUpdate(UpdateEvent event) {
		Position focusEntityPos = (Position) focusEntity.getComponent(Position.class);
		
		if(focusEntityPos == null)
			return;
		
		GameContainer gc = event.getGc();

		camera.setPosition(focusEntityPos.getX() - (gc.getWidth() / 2),
				focusEntityPos.getY() - (gc.getHeight() / 2));
	}

}
